<template>
  <div id="main">

    <h2>知识分享管理</h2>

    <el-button type="primary" style="margin:20px" @click="handleOpen('添加知识分享',1)">添加知识分享</el-button>

    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >

        <el-table-column
          label="序号"
          type="index"
          width="80"
          align="center"
        >
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="knowledgeUserId"
          label="知识分享人"
        />

        <el-table-column
          align="center"
          prop="knowledgePartId"
          label="知识分享学部"
        />

        <el-table-column align="center" label="操作">
          <template slot-scope="Scope">
            <!-- <el-button type="primary" icon="el-icon-edit" @click="handleOpen('修改知识分享',2,Scope.row.knowledgeId)">编辑</el-button> -->
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleOpen('修改知识分享',2,Scope.row.knowledgeId)">详情</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(Scope.row.knowledgeId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5,10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <el-dialog title="" :close-on-click-modal="false" :visible.sync="dialogFormVisible" :before-close="handleClose">
      <el-form ref="knowledge" :model="knowledge" label-position="top">

        <!-- <el-form-item prop="knowledgeId" label="知识分享Id" :label-width="formLabelWidth">
          <el-input v-model="knowledge.knowledgeId" autocomplete="off" :readonly="readonly" />
        </el-form-item> -->
        <el-form-item prop="knowledgeUserId" label="知识分享人Id" :label-width="formLabelWidth">
          <el-input v-model="knowledge.knowledgeUserId" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="knowledgePartId" label="知识分享学部Id" :label-width="formLabelWidth">
          <el-input v-model="knowledge.knowledgePartId" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="knowledgeContent" label="知识分享内容" :label-width="formLabelWidth">
          <el-input v-model="knowledge.knowledgeContent" type="textarea" :autosize="{ minRows: 5, maxRows: 100}" autocomplete="off" maxlength="4000" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose">取 消</el-button>
        <!-- <el-button v-if="editType==2" type="primary" @click="handleUpdate()">修 改</el-button> -->
        <el-button v-if="editType==1" type="primary" @click="handleAddUser()">添 加</el-button>
      </div>
    </el-dialog>

  </div>

</template>
<script>
import { addKnowledge, deleteKnowledge, getKnowledge, getKnowledgeById } from '@/api/knowledge'
export default {

  data() {
    return {
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      list: [],
      listLoading: false,
      dialogFormVisible: false,
      editType: 1, // 1 添加 2 修改
      knowledge: {},
      formLabelWidth: '200px',
      readonly: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      this.listLoading = true
      getKnowledge().then((data) => {
        this.list = data.obj
        this.listLoading = false
      })
    },
    handleDelete(id) {
      this.$confirm('确认删除该知识分享？').then(() => deleteKnowledge(id).then((data) => {
        this.$notify({
          title: '操作成功',
          type: 'success'
        })
        const totalPage = Math.ceil((this.list.length - 1) / this.pagesize)
        const page = this.currentPage > totalPage ? totalPage : this.currentPage
        this.currentPage = this.currentPage < 1 ? 1 : page
        this.fetchData()
      }))
    },
    handleAddUser() {
      addKnowledge(this.knowledge).then((resp) => {
        this.$notify({
          title: '操作成功',
          type: 'success'
        })
        this.fetchData()
      })
      this.knowledge = {}
      this.dialogFormVisible = false
    },
    handleOpen(title, type, id) {
      this.readonly = false
      this.knowledge = {}
      this.dialogFormVisible = true
      this.title = title
      this.editType = type
      if (type === 2) {
        this.readonly = true
        getKnowledgeById(id).then((resp) => {
          console.log(resp)
          this.knowledge = resp.obj
          console.log(this.knowledge)
        })
      }
    },
    handleClose() {
      this.dialogFormVisible = false
      this.knowledge = {}
    }
    // handleUpdate() {
    //   console.log(this.staff)
    //   updateStaff(this.staff).then((resp) => {
    //     this.$notify({
    //       title: '修改成功',
    //       type: 'success'
    //     })
    //     this.fetchData()
    //   })
    //   this.dialogFormVisible = false
    // }
  }

}
</script>

<style scoped>
#main{
  margin-left: 5px;
  margin-right: 5px;
}
#table-area{
  margin-left: 10px;
  margin-right: 10px;
}
</style>
